/**
 * @ProjectName:Backend
 * @Title: GlobalExceptionController
 * @Package com.phrp.exception
 * @Description: 全局异常处理控制类：处理各类异常的业务
 * @author SuShuChao
 * @date 2024/4/17 13:58
 * @version V1.0
 * Copyright (c) 2024, 2034279227@qq.com All Rights Reserved.
 */
package com.phrp.exception;

import com.phrp.util.returnResult.ResponseResult;
import com.phrp.util.returnResult.ReturnInfoEnum;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@ControllerAdvice
public class GlobalExceptionController {
    /**
     * 处理自定义的业务异常
     * @param req
     * @param exception
     * @return
     */
    @ExceptionHandler(value = CustomizationException.class)
    @ResponseBody
    public ResponseResult exception(HttpServletRequest req, CustomizationException exception) {
        return ResponseResult.error(exception.getErrorCode(), exception.getErrorMsg());
    }

    /**
     * 处理空指针的异常
     * @param req
     * @param e
     * @return
     */
    @ExceptionHandler(value = NullPointerException.class)
    @ResponseBody
    public ResponseResult exceptionHandler(HttpServletRequest req, NullPointerException e) {
        return ResponseResult.error(ReturnInfoEnum.BODY_NOT_MATCH);
    }
}
